package com.opensource.afuluo.valve.integration.db;

import cn.hutool.core.lang.Console;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSON;
import com.opensource.afuluo.annotation.DBTable;
import com.opensource.afuluo.valve.integration.db.condition.CommonCondition;
import com.opensource.afuluo.valve.integration.model.AutoExecPlanDO;
import com.opensource.afuluo.valve.integration.model.CommonDO;
import com.opensource.afuluo.valve.integration.model.TestTableDO;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class DBProxyUtils {

    public static <T> T findOne(Class<T> clazz, CommonCondition condition) throws SQLException {

        List<T> lists = DBProxyUtils.findAll(clazz, condition);
        if (!CollectionUtils.isEmpty(lists)) {
            return lists.get(0);
        }
        return null;
    }

    public static <T> List<T> findAll(Class<T> clazz, CommonCondition condition) throws SQLException {
        DBTable dbTable = clazz.getAnnotation(DBTable.class);
        if (null == dbTable) {
            // 异常处理
        }
        List<Entity> entities = null;
        if (null == condition) {
            entities = Db.use().findAll(dbTable.value());
        } else {
            entities = Db.use().find(Entity.create(dbTable.value()).parseBean(condition, true, true));
        }
        if (!CollectionUtils.isEmpty(entities)) {
            return entities.stream().map(entity -> entity.toBean(clazz)).collect(Collectors.toList());
        }
        return null;
    }

    public static <T> List<T> findByPage(Class<T> clazz, CommonCondition condition, int cursor, int size) throws SQLException {
        DBTable dbTable = clazz.getAnnotation(DBTable.class);
        if (null == dbTable) {
            // 异常处理
        }
        List<Entity> entities = null;
        if (null == condition) {
            entities = Db.use().page(Entity.create(dbTable.value()), cursor, size);
        } else {
            entities = Db.use().page(Entity.create(dbTable.value()).parseBean(condition, true, true), cursor, size);
        }
        if (!CollectionUtils.isEmpty(entities)) {
            return entities.stream().map(entity -> entity.toBean(clazz)).collect(Collectors.toList());
        }
        return null;
    }

    public static int insert(CommonDO commonDO) throws SQLException {
        DBTable dbTable = commonDO.getClass().getAnnotation(DBTable.class);
        if (null == dbTable) {
            // 异常处理
        }
        int cnt = Db.use().insert(Entity.create(dbTable.value()).parseBean(commonDO, true, true));
        if (cnt < 1) {
            // 异常处理
        }
        return cnt;
    }

    public static long insertWithinKey(CommonDO commonDO) throws SQLException {
        DBTable dbTable = commonDO.getClass().getAnnotation(DBTable.class);
        if (null == dbTable) {
            // 异常处理
        }
        Long cnt = Db.use().insertForGeneratedKey(Entity.create(dbTable.value()).parseBean(commonDO, true, true));
        if (null == cnt) {
            // 异常处理
        }
        return cnt;
    }

    public static int delete(CommonDO commonDO) throws SQLException {
        DBTable dbTable = commonDO.getClass().getAnnotation(DBTable.class);
        if (null == dbTable) {
            // 异常处理
        }
        int cnt = Db.use().del(Entity.create(dbTable.value()).parseBean(commonDO, true, true));
        if (cnt < 1) {
            // 异常处理
        }
        return cnt;
    }

    public static int update(CommonDO commonDO, CommonCondition commonCondition) throws SQLException {
        DBTable dbTable = commonDO.getClass().getAnnotation(DBTable.class);
        if (null == dbTable) {
            // 异常处理
        }
        int cnt = Db.use().update(Entity.create().parseBean(commonDO, true, true), Entity.create(dbTable.value()).parseBean(commonCondition, true, true));
        if (cnt < 1) {
            // 异常处理
        }
        return cnt;
    }

    public static void main(String[] args) throws Exception {
        TestTableDO testTableDO = DBProxyUtils.findOne(TestTableDO.class, null);
        System.out.println(JSON.toJSONString(testTableDO));
        DBProxyUtils.insert(new TestTableDO().setName("李四"));

        AutoExecPlanDO planDO = DBProxyUtils.findOne(AutoExecPlanDO.class, null);
        System.out.println(JSON.toJSONString(planDO));

        AutoExecPlanDO newPlan = new AutoExecPlanDO()
                .setGmtCreate(new Date())
                .setGmtModified(new Date())
                .setPlanId("aaaa")
                .setStatus(1)
                .setResource("MOCK")
                .setResourceType("FILE");

        Long id = DBProxyUtils.insertWithinKey(newPlan);
        Console.log(id);
    }
}
